package com.zyb.lmbackend.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.lmbackend.entity.GoodsBack;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface GoodsBackMapper {

    @Select("/* 分页读取原始退货明细，按 SN 排序便于稳定分页 */ SELECT back_detail, sn FROM goods_back ORDER BY sn ASC")
    Page<GoodsBack> selectPageOrdered(Page<GoodsBack> page);

    @Select("/* 统计原始退货明细总数 */ SELECT COUNT(*) FROM goods_back")
    long countAll();
    /*ORDER BY RAND()把所有的数据做一次随机排序，之后取出限制的条数，做到随机抽样数据返回*/
    @Select("/* 随机抽样若干退货原因用于归纳 */ SELECT back_detail FROM goods_back ORDER BY RAND() LIMIT #{limit}")
    List<String> selectRandomBackDetails(@Param("limit") int limit);

    @Select("/* 根据 SN 查询一条退货明细 */ SELECT back_detail, sn FROM goods_back WHERE sn = #{sn} LIMIT 1")
    GoodsBack selectBySn(@Param("sn") String sn);

    @Select("/* 选择当前版本尚未分类的记录 */ SELECT g.back_detail, g.sn FROM goods_back g LEFT JOIN goods_back_classified c ON c.sn = g.sn AND c.task_version = #{version} WHERE c.id IS NULL ORDER BY g.sn ASC LIMIT #{limit}")
    List<GoodsBack> selectUnclassified(@Param("version") int version, @Param("limit") int limit);
}
